<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

// +----------------------------------------------------------------------+
// | Treacle PHP Framework Version 0.10                                   |
// +----------------------------------------------------------------------+
// | Copyright (c) 2007 Treacle.cn All rights reserved.					  |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the 'License');      |
// | you may not use this file except in compliance with the License.     |
// | You may obtain a copy of the License at                              |
// | http://www.apache.org/licenses/LICENSE-2.0                           |
// | Unless required by applicable law or agreed to in writing, software  |
// | distributed under the License is distributed on an 'AS IS' BASIS,    |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
// | implied. See the License for the specific language governing         |
// | permissions and limitations under the License.                       |
// +----------------------------------------------------------------------+
// | Author: BoPo <ibopo@126.com>										  |
// +----------------------------------------------------------------------+
/**
 * Treacle apc_driver class file
 *
 * @author BoPo <ibopo@126.com>
 * @link http://www.treacle.cn/
 * @copyright Copyright &copy; 2007 Treacle
 * @license http://www.treacle.cn/license/
 * @version $Id$
 * @package caching
 */
/**
 * apc 缓存驱动
 *
 * @package caching
 * @author BoPo <ibopo@126.com>
 * @since 0.10
 * @version	$Id $
 */
class apc_driver extends Cache_driver
{
   /**
    * 构造函数,初始化apc的设置
    */
	public function __construct()
	{
		if(!extension_loaded('apc'))
		{
			throw new TSystemException('apccache_extension_required');
		}
		if(ini_get('apc.enabled') == false)
		{
			throw new TSystemException('apccache_extension_not_enabled');
		}
		if(substr(php_sapi_name(), 0, 3) === 'cli' and ini_get('apc.enable_cli') == false)
		{
			throw new TSystemException('apccache_extension_not_enabled_cli');
		}
		parent::__construct();
	}
	/**
	 * 获取一个标识的值
	 * @param string $key
	 * @return string
	 */
	protected function getValue($key)
	{
		return unserialize(apc_fetch($key));
	}
	/**
	 * 设置一个标识的值
	 *
	 * @param string the key identifying the value to be cached
	 * @param string the value to be cached
	 * @param integer the number of seconds in which the cached value will expire. 0 means never expire.
	 * @return boolean true if the value is successfully stored into cache, false otherwise
	 */
	protected function setValue($key,$value,$expire)
	{
		return apc_store($key,serialize($value),$expire);
	}
	/**
	 * 追加一个标识的值,并设置期限
	 *
	 * @param string the key identifying the value to be cached
	 * @param string the value to be cached
	 * @param integer the number of seconds in which the cached value will expire. 0 means never expire.
	 * @return boolean true if the value is successfully stored into cache, false otherwise
	 */
	protected function addValue($key,$value,$expire)
	{
		if(function_exists('apc_add')) 
		{
			return apc_add($key,serialize($value),$expire);
		} 
		else 
		{
			throw new TSystemException('apccache_add_unsupported');
		}
	}
	/**
	 * 删除一个标识的值
	 * 
	 * @param string the key of the value to be deleted
	 * @return boolean if no error happens during deletion
	 */
	protected function deleteValue($key)
	{
		return apc_delete($key);
	}
	/**
	 * 清空缓存中的所有内容
	 */
	public function flush()
	{
		return apc_clear_cache('user');
	}
}
/*
+---------------------------------------------------------------------------
|   Last Modified By BoPo
|   Date: Thu Sep 27 17:38:27 CST 2007
+--------------------------------------------------------------------------
*/
?>